package com;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ToJSONController {
    @RequestMapping("/toJSON")
    @ResponseBody //加上这个注解表示不会走视图解析器 也就是不会跳转页面
    public String toJSON() throws JsonProcessingException {
        User user = new User("王",3,"男");
        //json 工具类
        ObjectMapper objectMapper =new ObjectMapper();
        String value = objectMapper.writeValueAsString(user);
        //返回json字符串
        return value;
    }
}
